package cn.com.lyj6851.modules.activity.service.impl;

import cn.com.lyj6851.common.service.impl.BaseServiceImpl;
import cn.com.lyj6851.dozer.service.IGenerator;
import cn.com.lyj6851.modules.activity.domain.YdyStoreBargainUser;
import cn.com.lyj6851.modules.activity.domain.YdyStoreBargainUserHelp;
import cn.com.lyj6851.modules.activity.service.YdyStoreBargainUserHelpService;
import cn.com.lyj6851.modules.activity.service.YdyStoreBargainUserService;
import cn.com.lyj6851.modules.activity.service.mapper.YdyStoreBargainUserHelpMapper;
import cn.com.lyj6851.modules.activity.vo.YdyStoreBargainUserHelpQueryVo;
import cn.com.lyj6851.modules.user.domain.YdyUser;
import cn.com.lyj6851.modules.user.service.YdyUserService;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 砍价用户帮助表 服务实现类
 * </p>
 *
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class YdyStoreBargainUserHelpServiceImpl extends BaseServiceImpl<YdyStoreBargainUserHelpMapper, YdyStoreBargainUserHelp> implements YdyStoreBargainUserHelpService {

    @Autowired
    private IGenerator generator;

    @Autowired
    private YdyStoreBargainUserHelpMapper yxStoreBargainUserHelpMapper;

    @Autowired
    private YdyStoreBargainUserService storeBargainUserService;
    @Autowired
    private YdyUserService userService;




    /**
     * 获取砍价帮
     * @param bargainId 砍价商品id
     * @param bargainUserUid 砍价用户id
     * @param page page
     * @param limit limit
     * @return list
     */
    @Override
    public List<YdyStoreBargainUserHelpQueryVo> getList(Long bargainId, Long bargainUserUid,
                                                        int page, int limit) {
        YdyStoreBargainUser storeBargainUser = storeBargainUserService
                .getBargainUserInfo(bargainId,bargainUserUid);
        if(ObjectUtil.isNull(storeBargainUser)) {
            return Collections.emptyList();
        }
        Page<YdyStoreBargainUserHelp> pageModel = new Page<>(page, limit);
        QueryWrapper<YdyStoreBargainUserHelp> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(YdyStoreBargainUserHelp::getBargainUserId,storeBargainUser.getId())
                .orderByDesc(YdyStoreBargainUserHelp::getId);
        List<YdyStoreBargainUserHelpQueryVo> storeBargainUserHelpQueryVos = generator
                .convert(yxStoreBargainUserHelpMapper.selectPage(pageModel,wrapper).getRecords(),
                        YdyStoreBargainUserHelpQueryVo.class);

        storeBargainUserHelpQueryVos.forEach(item->{
            YdyUser yxUser = userService.getById(item.getUid());
            item.setAvatar(yxUser.getAvatar());
            item.setNickname(yxUser.getNickname());
        });

        return storeBargainUserHelpQueryVos;
    }

    /**
     * 获取砍价帮总人数
     * @param bargainId 砍价产品ID
     * @param bargainUserUid 用户参与砍价表id
     * @return int
     */
    @Override
    public int getBargainUserHelpPeopleCount(Long bargainId, Long bargainUserUid) {
        return this.lambdaQuery()
                .eq(YdyStoreBargainUserHelp::getBargainUserId,bargainUserUid)
                .eq(YdyStoreBargainUserHelp::getBargainId,bargainId)
                .count();
    }
}
